package gov.nist.applet.phone.security.filetransfer;



import gov.nist.applet.phone.security.DESEncoder;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

public class SendFile
{
	int port = 8077;
	String filePath = null;

	/**
	 * start send thread to wait client to receive the data.
	 */
	public void start()
	{
		Socket s = null;
		try
		{
			ServerSocket ss = new ServerSocket(port);
			while (true)
			{
				// 选择进行传输的文件
				//String filePath = "D:/test.txt";
				filePath = getFilePath();
				System.out.println(filePath);
				File fi = new File(filePath);
				System.out.println("文件长度:" + (int) fi.length());

				// public Socket accept() throws
				// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。

				s = ss.accept();
				System.out.println("建立socket链接");


				//经过双缓冲流的包装可以提高流写的速度，不用每次都刷新缓冲区
				BufferedReader bf = new BufferedReader(new InputStreamReader(
						new BufferedInputStream(new FileInputStream(filePath))));
				BufferedWriter bw = new BufferedWriter(
						new OutputStreamWriter(new BufferedOutputStream(s.getOutputStream())));

				DataOutputStream ps = new DataOutputStream(s.getOutputStream());
				ps.writeUTF(fi.getName());
				ps.flush();
				ps.writeLong((long) fi.length());
				ps.flush();
				//ps.close();

				int bufferSize = 1024*4;
				byte[] buf = new byte[bufferSize];
				String str = null;
				//在往网络中写数据的时候，服务器这边是等待客户端链接上才会开始写，如果没有客户端连接
				//线程就一直监听，当连接上，就往缓冲区写，写满的时候要客户端取走，才开始再写，
				while(true)
				{
					str = bf.readLine();
					
					if("".equals(str)||null==str)
					{
						break;
					}
					buf = str.getBytes("UTF-8");
					System.out.println(str);
					// 对字节数组里面的数据进行加密
					DESEncoder desEncoder = new DESEncoder("123456");
					str = desEncoder.encryptStr(str);
					System.out.println(str);
					
					bw.write(str);
					bw.newLine();
				}
				bf.close();
				bw.close();
				s.close();
				JOptionPane.showMessageDialog(null,"文件传输完成","消息",JOptionPane.INFORMATION_MESSAGE);
				System.out.println("文件传输完成");
				//如果没有return，则服务器一直监听这个端口，等待客户端的链接。
				return;
			}

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * return the file path 
	 * @return path-String
	 */
	public String getFilePath()
	{	
		return filePath;
	}
	
	/**
	 * set the file path
	 * @param path
	 */
	public void setFilePath(String path)
	{
		this.filePath = path;
	}
	/**
	 * set the listen port:
	 * @param port
	 */
	public void setPort(int port)
	{
		this.port = port;
	}
	/**
	 * get the listen port:
	 * @return
	 */
	public int getPort()
	{
		return this.port;
	}

	public static void main(String arg[])
	{
		new SendFile().start();
	}
}